package com.wyd.System.Controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyd.System.Bean.Students;
import com.wyd.System.Service.StudentsService;
import com.wyd.System.Vo.StudentsVo;
import com.wyd.response.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 文永达
 * @since 2020-12-25
 */
@RestController
@RequestMapping("/System/students")
@Api(value = "学生管理")
public class StudentsController {

    @Autowired
    private StudentsService studentsService;

    /*
    * 分页查询用户列表
    *
    * @return
    * */
    @ApiOperation("分页查询用户列表")
    @PostMapping("/findStudentPage")
    public Result findStudentPage(@RequestParam(required = true,defaultValue = "1")Integer current,
                                  @RequestParam(required = true,defaultValue = "6")Integer size,
                                  @RequestBody StudentsVo studentsVo){
        Page<Students> page = new Page<>(current,size);
        QueryWrapper<Students> wrapper= getWrapper(studentsVo);
        IPage<Students> studentPage = studentsService.findStudentPage(page,wrapper);
        long total = studentPage.getTotal();
        List<Students> records = studentPage.getRecords();
        return Result.ok().data("total",total).data("records",records);
    }

    private QueryWrapper<Students> getWrapper(StudentsVo studentsVo){
        QueryWrapper<Students> queryWrapper = new QueryWrapper<>();
        if(studentsVo!=null){
            if(!StringUtils.isEmpty(studentsVo.getStuGrade())){
                queryWrapper.eq("stu_grade",studentsVo.getStuGrade());
            }
            if(!StringUtils.isEmpty(studentsVo.getStuInstitute())){
                queryWrapper.eq("stu_institute",studentsVo.getStuInstitute());
            }
            if(!StringUtils.isEmpty(studentsVo.getStuSpecialty())){
                queryWrapper.eq("stu_specialty",studentsVo.getStuSpecialty());
            }
            if(!StringUtils.isEmpty(studentsVo.getStuClazz())){
                queryWrapper.eq("stu_clazz",studentsVo.getStuClazz());
            }
            if(!StringUtils.isEmpty(studentsVo.getStuId())){
                queryWrapper.eq("stu_id",studentsVo.getStuId());
            }
            if(!StringUtils.isEmpty(studentsVo.getStuName())){
                queryWrapper.eq("stu_name",studentsVo.getStuName());
            }
            if(!StringUtils.isEmpty(studentsVo.getStuGender())){
                queryWrapper.eq("stu_gender",studentsVo.getStuGender());
            }
        }
        return queryWrapper;
    }

    @ApiOperation(value = "添加学生",notes = "添加学生")
    @PostMapping("/addStudent")
    public Result addStudent(@RequestBody Students students){
        try{
            studentsService.addStudent(students);
            return Result.ok();
        }catch (Exception e){
            //打印日志
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation(value = "删除学生",notes = "删除学生信息，根据学生ID")
    @DeleteMapping("/delete/{id}")
    public Result deleteStudentById(@PathVariable Integer id){
        try{
            studentsService.deleteStudentById(id);
            return Result.ok();
        }catch (Exception e){
            //打印日志
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation(value = "更新学生",notes = "更新学生信息，根据学生ID")
    @PutMapping("/update/{id}")
    public Result updateStudentById(@PathVariable Integer id,@RequestBody Students students){
        try {
            studentsService.updateStudentById(id,students);
            return Result.ok();
        }catch (Exception e){
            //打印日志
            e.printStackTrace();
            return Result.error();
        }
    }
}

